#include <GridHelper>
#include <Document>
#include <Core>
#include <QtOpenGL>

GridHelper::GridHelper(Document &doc, QObject *parent) :
    Helper(parent)
  , m_doc(doc)
{

}

void GridHelper::draw() const
{
    if(!isActive()) return;

    glPushMatrix();
    glLoadIdentity();

    boost::shared_ptr<Renderer> renderer = Core::instance().renderer();

    float gcs = m_doc.gridCellSize();
    QWidget* pView = m_doc.ownerView();
    QRect area = pView->rect();

    renderer->enableBlending(Renderer::AlphaOpacity);

    for(float h = gcs;h < area.width();h += gcs)
    {
        QPointF start(h, 0), end(h, area.height());
        renderer->drawLine(start, end, QColor::fromRgb(0, 0, 255, 128));
    }

    for(float w = gcs;w < area.height();w += gcs)
    {
        QPointF start(0, w), end(area.width(), w);
        renderer->drawLine(start, end, QColor::fromRgb(0, 0, 255, 128));
    }

    renderer->disableBlending();

    glPopMatrix();
}
